
% Function to calculate maximun rate of climb
%
% h = Altitude [m]
% W = Weight [N]
% Throttle [%]
% flag = 1 for jet
% flag = 0 for turboprop
% n = number of engines
%
% RD_min_gamma  = Rate of descent of minimum glide angle [m/s]
% tas_min_gamma = True air speed of minimum glide angle [m/s]

function [RD_min_gamma, tas_min_gamma] = calc_RD(h, W, Throttle, ISA, flag, n)

global ac oswald

S = ac.geo.wing.S;
AR = ac.geo.wing.ar;
eps = 1e-2;

[p,rho,mu,T,sound,delta_isa] = isa_cos(h,ISA,1);

% Loop to find tas_min_gamma
tas = 100;
Cd0 = calc_Cd0_aircraft(ac,h,tas);
Cl = sqrt(Cd0*pi*AR*oswald); % Cl for smallest glide angle. Roskam performance pg.337
tas_aux = sqrt(2*W/(rho*S*Cl));

while abs(tas-tas_aux) > eps
    tas = tas_aux;
    Cd0 = calc_Cd0_aircraft(ac,h,tas);
    Cl = sqrt(Cd0*pi*AR*oswald);
    tas_aux = sqrt(2*W/(rho*S*Cl));
end

tas_min_gamma = tas;
Cd = Cd0 + Cl^2/(pi*AR*oswald);
RD_min_gamma = sqrt(W*2*Cd^2/(S*rho*Cl^3));


